# SVG转PNG
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
from PIL import Image

from die import Die

import pygal
a = int(input('请输入骰子1的面数：'))
b = int(input('请输入骰子2的面数：'))
die_1 = Die(a)
die_2 = Die(b)
cnt = int(input("请输入需要掷骰子的次数："))
results = []
for roll_num in range(cnt):
    result = die_1.roll() + die_2.roll()
    results.append(result)

frequencies = []
total_sum = 0
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result + 1):
    frequency = results.count(value)
    total_sum += frequency
    frequencies.append(frequency)

freq = []
num = []
for value in range(2, max_result + 1):
    div = results.count(value)/total_sum*100
    freq.append(div)
    num.append(value)
    print("点数"+str(value)+"的概率是："+str(div)+'%')

hist = pygal.Bar()

hist.title = "Results of rolling a D6 and a D10 50,000 times."
hist.x_labels = num[:]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"

hist.add('D10 + D10', frequencies)
hist.render_to_file('15.4.8_dice_visual.svg')

# SVG转png
drawing = svg2rlg('15.4.8_dice_visual.svg')
renderPM.drawToFile(drawing, '15.4.8_dice_visual.png', fmt="PNG")
img = Image.open('15.4.8_dice_visual.png')
img.show()
